
import numpy as np
from scipy.integrate import quad,nquad
import sys,io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
from sympy import symbols, diff, sin, cos, ln, Function, solve


# 定义被积函数
def integrand(x):
    return np.sin(x)

# 计算定积分
result, error = quad(integrand, 0, np.pi/2)
print(f"积分结果: {result}",f"误差估计: {error}")

# r1=nquad(lambda x:10*x+5,[x for x in range(10)])




def basic_derivative():
    """基本函数求导示例"""
    x = symbols('x')
    
    # 幂函数求导
    f1 = x**3
    deriv1 = diff(f1, x)
    print(f"d(x³)/dx = {deriv1}")
    
    # 三角函数求导
    f2 = sin(x)
    deriv2 = diff(f2, x)
    print(f"d(sin(x))/dx = {deriv2}")
    
    # 指数函数求导
    f3 = ln(x)
    deriv3 = diff(f3, x)
    print(f"d(ln(x))/dx = {deriv3}")

def implicit_derivative():
    """隐函数求导示例"""
    x, y = symbols('x y')
    y_func = Function('y')(x)
    
    # 定义隐函数方程: x² + y² - x*sin(y) = 1
    equation = x**2 + y_func**2 - x*sin(y_func) - 1
    
    # 隐函数求导
    derivative = diff(equation, x)
    dy_dx = solve(derivative, diff(y_func, x))
    print(f"隐函数导数: {dy_dx[0]}")

def logarithmic_derivative():
    """对数求导法示例"""
    x = symbols('x')
    y = x**sin(x)  # 幂指函数
    
    # 对数求导法
    ln_y = ln(y)
    deriv_ln = diff(ln_y, x)
    result = y * deriv_ln
    print(f"对数求导结果: {result.simplify()}")

def parametric_derivative():
    """参数方程求导示例"""
    t = symbols('t')
    x = sin(t)      # x(t) = sin(t)
    y = t**2 + 1    # y(t) = t² + 1
    
    dx_dt = diff(x, t)
    dy_dt = diff(y, t)
    dy_dx = dy_dt / dx_dt
    print(f"参数方程导数: {dy_dx}")

if __name__ == "__main__":
    print("=== 基本函数求导 ===")
    basic_derivative()
    
    print("\n=== 隐函数求导 ===")
    implicit_derivative()
    
    print("\n=== 对数求导法 ===")
    logarithmic_derivative()
    
    print("\n=== 参数方程求导 ===")
    parametric_derivative()
